package com.supcalls;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class DateTimeDemo {

    public static void main(String[] args) {
        // 获取当前日期和时间
        LocalDate today = LocalDate.now();
        LocalTime now = LocalTime.now();
        LocalDateTime dateTime = LocalDateTime.now();
        ZonedDateTime zonedDateTime = ZonedDateTime.now();

        // 输出当前日期、时间、日期时间和时区时间
        System.out.println("当前日期: " + today);
        System.out.println("当前时间: " + now);
        System.out.println("当前日期时间: " + dateTime);
        System.out.println("当前时区日期时间: " + zonedDateTime);

        // 创建指定日期和时间
        LocalDate specificDate = LocalDate.of(2025, Month.FEBRUARY, 4);
        LocalTime specificTime = LocalTime.of(14, 30);
        LocalDateTime specificDateTime = LocalDateTime.of(2025, Month.FEBRUARY, 4, 14, 30);

        System.out.println("指定日期: " + specificDate);
        System.out.println("指定时间: " + specificTime);
        System.out.println("指定日期时间: " + specificDateTime);

        // 日期和时间的加减
        LocalDate nextWeek = today.plusWeeks(1);
        LocalTime twoHoursLater = now.plusHours(2);
        LocalDateTime lastMonth = dateTime.minusMonths(1);

        System.out.println("一周后的日期: " + nextWeek);
        System.out.println("两小时后的时间: " + twoHoursLater);
        System.out.println("上个月的日期时间: " + lastMonth);

        // 格式化日期时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = dateTime.format(formatter);
        System.out.println("格式化后的日期时间: " + formattedDateTime);

        // 解析日期时间字符串
        String dateTimeStr = "2025-02-04 14:30:00";
        LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeStr, formatter);
        System.out.println("解析后的日期时间: " + parsedDateTime);

        // 计算持续时间
        Duration duration = Duration.between(now, twoHoursLater);
        System.out.println("时间差: " + duration.toMinutes() + " 分钟");

        // 计算两个日期之间的天数差
        long daysBetween = ChronoUnit.DAYS.between(specificDate, today);
        System.out.println("指定日期与当前日期的天数差: " + daysBetween + " 天");

        // 时区转换
        ZoneId zoneId = ZoneId.of("America/New_York");
        ZonedDateTime newYorkTime = ZonedDateTime.now(zoneId);
        System.out.println("纽约当前时间: " + newYorkTime);

        // 其他时区
        ZoneId tokyoZone = ZoneId.of("Asia/Tokyo");
        ZonedDateTime tokyoTime = ZonedDateTime.now(tokyoZone);
        System.out.println("东京当前时间: " + tokyoTime);
    }
}

